package com.ruoyi.system.controller;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.domain.TbUpeModel;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.TbUpeModelver;
import com.ruoyi.system.service.ITbUpeModelverService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 流程模型版本Controller
 * 
 * @author gxt
 * @date 2024-08-23
 */
@RestController
@RequestMapping("/system/modelver")
public class TbUpeModelverController extends BaseController
{
    @Autowired
    private ITbUpeModelverService tbUpeModelverService;

    /**
     * 查询流程模型版本列表
     */
    @PreAuthorize("@ss.hasPermi('system:modelver:list')")
    @GetMapping("/list")
    public TableDataInfo list(TbUpeModelver tbUpeModelver)
    {
        startPage();
        List<TbUpeModelver> list = tbUpeModelverService.selectTbUpeModelverList(tbUpeModelver);
        return getDataTable(list);
    }

    /**
     * 导出流程模型版本列表
     */
    @PreAuthorize("@ss.hasPermi('system:modelver:export')")
    @Log(title = "流程模型版本", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbUpeModelver tbUpeModelver)
    {
        List<TbUpeModelver> list = tbUpeModelverService.selectTbUpeModelverList(tbUpeModelver);
        ExcelUtil<TbUpeModelver> util = new ExcelUtil<TbUpeModelver>(TbUpeModelver.class);
        util.exportExcel(response, list, "流程模型版本数据");
    }

    /**
     * 获取流程模型版本详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:modelver:query')")
    @GetMapping(value = "/{wfid}")
    public AjaxResult getInfo(@PathVariable("wfid") String wfid)
    {
        return success(tbUpeModelverService.selectTbUpeModelverByWfid(wfid));
    }

    /**
     * 新增流程模型版本
     */
    @PreAuthorize("@ss.hasPermi('system:modelver:add')")
    @Log(title = "流程模型版本", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbUpeModelver tbUpeModelver)
    {
        return toAjax(tbUpeModelverService.insertTbUpeModelver(tbUpeModelver));
    }

    /**
     * 修改流程模型版本
     */
    @PreAuthorize("@ss.hasPermi('system:modelver:edit')")
    @Log(title = "流程模型版本", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbUpeModelver tbUpeModelver)
    {
        return toAjax(tbUpeModelverService.updateTbUpeModelver(tbUpeModelver));
    }

    /**
     * 删除流程模型版本
     */
    @PreAuthorize("@ss.hasPermi('system:modelver:remove')")
    @Log(title = "流程模型版本", businessType = BusinessType.DELETE)
	@DeleteMapping("/{wfids}")
    public AjaxResult remove(@PathVariable String[] wfids)
    {
        return toAjax(tbUpeModelverService.deleteTbUpeModelverByWfids(wfids));
    }


    @Log(title = "xml分析", businessType = BusinessType.IMPORT)
    @PostMapping("/importXml")
    public AjaxResult importXml(MultipartFile file){
        AjaxResult ajaxResult = new AjaxResult();
        try {
            // 解析xml
            TbUpeModel tbUpeModel = tbUpeModelverService.parseXml(file);
            ajaxResult.isSuccess();
            ajaxResult.put("data",tbUpeModel);
            ajaxResult.put("msg","解析成功");
            return ajaxResult;
        }catch (Exception e){
            e.printStackTrace();
            return error("解析失败");
        }
    }

    /**
     * 预览xml
     */
    @GetMapping("/preview/{wfid}")
    public AjaxResult preview(@PathVariable("wfid") String wfid) throws IOException
    {
        Map<String, String> dataMap = tbUpeModelverService.previewCode(wfid);
        return success(dataMap);
    }

}
